<template>
  <PageLayout>
    <template #header>
      <BasicForm :options="queryOptions" @ok="onQueryDataByParams"></BasicForm>
    </template>
    <div class="table-box">
      <div class="head-box">
        <div class="title">账号列表</div>
        <a-space :size="16">
          <a-button type="primary" @click="edit()">新增账号</a-button>
        </a-space>
      </div>
      <div class="bg-fff">
        <!-- :apiFetch="apiFetch"  -->
        <BasicTable
          ref="tableRef"
          :columns="columns"
          :query-params="queryData"
          :apiFetch="postUserPage"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'action'">
              <a-space class="space-btn--box" :size="10">
                <a class="btn" @click="edit(record)">编辑</a>
                <a class="btn" @click="remove(record.id)">删除</a>
                <a class="btn" v-if="!record.status" @click="changeStatus(record)">禁用</a>
                <a class="btn" v-else @click="changeStatus(record)">启用</a>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </div>
    </div>
  </PageLayout>
</template>

<script name="auth-account-list" setup>
import {
  postUserPage,
  postUserDelete,
  postUserUpdateStatus,
} from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { comfirmModal } from "@/hooks/useModal";
import { message } from "ant-design-vue";
const storeCommonEnum = useStoreCommonEnum();
const router = useRouter();
const queryOptions = ref([
  {
    name: "账号",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "username",
  },
  {
    name: "姓名",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "nickname",
  },
]);
const columns = [
  {
    title: "序号",
    dataIndex: "index",
    width: "80px",
    customRender: (record) => `${record.index + 1}`,
  },
  {
    title: "账号",
    dataIndex: "username",
    key: "username",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "姓名",
    dataIndex: "nickname",
    key: "nickname",
    width: "200px",
    ellipsis: true,
  },
  {
    title: "角色",
    dataIndex: "roleList",
    key: "roleList",
    // width: "150px",
    ellipsis: true,
    customRender: ({ record }) => {
      console.log('record.roleList',record.roleList);
      return record.roleList?.map(item => item.roleName).join('、') 
    },
  },
  // {
  //   title: "所属级别",
  //   dataIndex: "nickname",
  //   key: "nickname",
  //   width: "150px",
  //   ellipsis: true,
  // },
  // {
  //   title: "所属领导",
  //   dataIndex: "nickname",
  //   key: "nickname",
  //   width: "150px",
  //   ellipsis: true,
  // },
  {
    title: "上次登录时间",
    dataIndex: "loginTime",
    key: "loginTime",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "状态",
    dataIndex: "status",
    key: "status",
    width: "150px",
    ellipsis: true,
    customRender: ({ record }) => (!record.status ? "正常" : "禁用"),
  },

  {
    title: "操作",
    fixed: "right",
    dataIndex: "action",
    key: "action",
    width: 200,
  },
];

const queryData = reactive({});
const tableRef = ref(null);
const onQueryDataByParams = (params) => {
  for (let key in params) {
    queryData[key] = params[key];
  }
  tableRef.value.refreshTableData();
};

const edit = (record) => {
  router.push({
    path: "/auth/account/edit",
    query: {
      id: record?.id,
    },
  });
};

const changeStatus = (record) => {
  const title = !record.status  ? "禁用" : "启用";
  comfirmModal({
    content: `确认${title}该账号`,
    onOk() {
      postUserUpdateStatus({
        id: record.id,
        status: !record.status,
      }).then(() => {
        message.success(`${title}成功`);
        tableRef.value.fetch();
      });
    },
  });
};
const remove = (id) => {
  comfirmModal({
    content: "确认删除该账号",
    onOk() {
      postUserDelete({ id }).then(() => {
        message.success("删除成功");
        tableRef.value.fetch();
      });
    },
  });
};

</script>

<style lang="less" scoped></style>
